//
//  RedShowView.m
//  YaDiuDiu
//
//  Created by mac on 2017/10/18.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "RedShowView.h"

@interface RedShowView ()

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UILabel *tipsLabel;

@property (nonatomic, strong) UIView *lineView;

@property (nonatomic, strong) UIButton *cancelButton;

@property (nonatomic, strong) UIButton *OkButton;

@end

@implementation RedShowView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

+ (RedShowView *)shared{
    static RedShowView *red = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        red = [[RedShowView alloc]init];
    });
    return red;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupView];
        
        [self addConstraint];

    }
    return self;
}

- (void)setupView{
    [self setFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    [self setBackgroundColor:[UIColor clearColor]];
    
    self.bgView = [[UIView alloc]init];
    [self.bgView setBackgroundColor:[UIColor blackColor]];
    [self.bgView setAlpha:0.5];
    [self addSubview:self.bgView];
    
    self.contentView = [[UIView alloc]init];
    [self.contentView setBackgroundColor:[UIColor whiteColor]];
    [self.contentView.layer setMasksToBounds:YES];
    [self.contentView.layer setCornerRadius:5];
    [self addSubview:self.contentView];
    
    self.titleLabel = [[UILabel alloc]init];
    [self.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [self.titleLabel setTextColor:HexColorInt32_t(FD1142)];
    [self.contentView addSubview:self.titleLabel];
    
    self.tipsLabel = [[UILabel alloc]init];
    [self.tipsLabel setTextColor:HexColorInt32_t(333333)];
    [self.tipsLabel setFont:[UIFont systemFontOfSize:14]];
    [self.tipsLabel setText:@"已放入“我的红包”点击查看详情"];
    [self.contentView addSubview:self.tipsLabel];
    
    self.cancelButton = [[UIButton alloc]init];
    [self.cancelButton setBackgroundColor:[UIColor whiteColor]];
    [self.cancelButton setTitleColor:HexColorInt32_t(333333) forState:UIControlStateNormal];
    [self.cancelButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelButton addTarget:self action:@selector(cancelEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.cancelButton];
    
    self.OkButton = [[UIButton alloc]init];
    [self.OkButton setBackgroundColor:HexColorInt32_t(FD1142)];
    [self.OkButton setTitle:@"去看看" forState:UIControlStateNormal];
    [self.OkButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [self.OkButton addTarget:self action:@selector(okEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.OkButton];
    
    self.lineView = [[UIView alloc]init];
    [self.lineView setBackgroundColor:HexColorInt32_t(e6e6e6)];
    [self.contentView addSubview:self.lineView];
}

- (void)addLabelStyle:(NSString *)money{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"成功抢到红包：%@元",money]];
    [str addAttribute:NSForegroundColorAttributeName value:HexColorInt32_t(333333) range:NSMakeRange(0,7)];
    [self.titleLabel setAttributedText:str];
}

- (void)addConstraint{
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(0));
        make.left.equalTo(@(0));
        make.right.equalTo(@(0));
        make.bottom.equalTo(@(0));
    }];
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.equalTo(320);
        make.height.equalTo(160);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(30);
        make.left.equalTo(20);
        make.right.equalTo(-10);
        make.height.equalTo(20);
    }];
    
    [self.tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(70);
        make.left.equalTo(20);
        make.right.equalTo(-10);
        make.height.equalTo(20);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(115);
        make.left.equalTo(0);
        make.right.equalTo(0);
        make.height.equalTo(1);
    }];
    
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(115);
        make.left.equalTo(0);
        make.width.equalTo(160);
        make.height.equalTo(45);
    }];
    
    [self.OkButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(115);
        make.right.equalTo(0);
        make.width.equalTo(160);
        make.height.equalTo(45);
    }];
}

- (void)showTitle:(NSString *)title{
    [self addLabelStyle:title];
    [KWindow addSubview:self];
}

- (void)okEvent:(UIButton *)sender{
    self.redButtonBlock(sender);
    [self removeFromSuperview];
}

- (void)cancelEvent:(UIButton *)sender{
    self.redCancelButtonBlock(sender);
    [self removeFromSuperview];
}

@end
